package com.huynhchihieu.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.datamatrix.DataMatrixWriter;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.encoder.QRCode;

public class test {
	public static void main(String[] args) throws NotFoundException, ChecksumException, FormatException, IOException, WriterException {

		test t = new test();
		String text = "{docinfo:{filename:'File123.txt',doctype:'TreatmentDx',category:'Accn',categoryid: '126',orderid:'REQ126',testids:'300^151^CBC^120',comment:'This is a comment for File123.pdf file and multiple tests (300^151^CBC^120)'},splitinfo:{[startpage:'1',endpage:'3',"
				+ "category:'Accn',categoryid:'122',orderid:'REQ122'],"
				+ "[startpage:'4',endpage:'6',category:'Accn',"
				+ "categoryid:'154',orderid:'REQ154']}}"; // this is the text that we want to encode
		String fileName = "D:/Hieu/qrcode_97802017507991.png";
		String imageFormat = "png"; // could be "gif", "tiff", "jpeg"
		int width = 400;
		int height = 300; // change the height and width as per your requirement
		t.writeBarCode(text, fileName,imageFormat, width, height);
		Result result = t.readBarCode(fileName);
		System.out.println("Barcode text is " + result.getText());
	}
	
	public void writeBarCode(String text, String fileName,String imageFormat, int width,int height) throws WriterException, FileNotFoundException, IOException{
		// Represents a 2D matrix of bits
		BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
		//new com.google.zxing.datamatrix.DataMatrixWriter().encode(text, format, width, height)
		MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File(fileName)));
	}
	
	public Result readBarCode(String fileName) throws IOException, NotFoundException, ChecksumException, FormatException{
		InputStream barCodeInputStream = new FileInputStream(fileName);
		BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
		 
		LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
		Reader reader = new MultiFormatReader();
		Result result = reader.decode(bitmap);
		return result; 
	}
}
